
<script>
  var myDate='<%=  (@monday).to_s -%>';
  var myHeader="Nombre,%,Estado,Lu(<%=  @monday.to_s(:short) -%>),Ma(<%=  (@monday+1).to_s(:short) -%>),Mi(<%=  (@monday+2).to_s(:short) -%>),Ju(<%=  (@monday+3).to_s(:short) -%>),Vi(<%=  (@monday+4).to_s(:short) -%>),Sa(<%=  (@monday+5).to_s(:short) -%>),Do(<%=  (@monday+6).to_s(:short) -%>),suma";
  var cnt=0;
  var interval=1500;
  var status="N/A";

  function actualiza() {
    initGrid();
  }

  function initGrid(){
	mygrid = new dhtmlXGridObject('gridbox');
        mygrid.setImagePath("/imgs/");
        mygrid.setHeader(myHeader);
        mygrid.setInitWidths("130,50,90,90,90,90,90,90,90,90,90");
        mygrid.setColAlign("left,left,left,center,center,center,center,center,center,center,center");
        mygrid.setColTypes("ro,ed,coro,ed,ed,ed,ed,ed,ed,ed,ro");
        mygrid.setSkin("dhx_skyblue");
        mygrid.setColSorting("str,str,str,str,str,str,str,str,str,str,str");

        mygrid.attachEvent("onEnter", actualiza );
        mygrid.attachEvent("onEditCell", doOnCellEdit );

        
        mygrid.init();
        mygrid.loadXML("/accepted/show_all/<%= params[:id].to_i.to_s %>.xml?start_date="+myDate);
        myDataProcessor = new dataProcessor("/week_time_sheets/up/1.xml");
        myDataProcessor.setUpdateMode("cell");
        myDataProcessor.init(mygrid);
        setTimeout("autoSuma()",interval);
        updateStatus();
  }


function updateStatus(){
      <%= remote_function :url => {:action => :status}, :with => " 'start_date='+myDate+'&id="+@user.id.to_s+"'" %>
}


function callBack(){
      document.getElementById("statusDiv").innerHTML=status;
}



function doOnCellEdit(stage, rowId, cellInd) {

    if (stage == 0) {
    } else if (stage == 1  ) {

    } else if (stage == 2  && (cellInd>=3 )) { //si la celda fue modificada y me aseguro que sean la c3>

				mygrid.cells(rowId,10).setValue(
                                parseInt(mygrid.cells(rowId,3).getValue())
                                + parseInt(mygrid.cells(rowId,4).getValue())
                                + parseInt(mygrid.cells(rowId,5).getValue())
                                + parseInt(mygrid.cells(rowId,6).getValue())
                                + parseInt(mygrid.cells(rowId,7).getValue())
                                + parseInt(mygrid.cells(rowId,8).getValue())
                                + parseInt(mygrid.cells(rowId,9).getValue())
                                );

                                autoSuma();


    }else if ( stage == 2 && cellInd == 1 ){

       precentage=parseInt( mygrid.cells(rowId,1).getValue());
       if ( precentage == 0){
         mygrid.cells(rowId,2).setValue("Nuevo");
       }else if( precentage > 0 && precentage<100 ) {
         mygrid.cells(rowId,2).setValue("En proceso");
       }else{
         mygrid.cells(rowId,2).setValue("Completado");
       }
       
     }else if( stage == 2 && cellInd == 2 ){
        estado = mygrid.cells(rowId,2).getValue().toString();
        if(estado == "Nuevo"){
          mygrid.cells(rowId,1).setValue(0);
        }else if(estado == "Completado"){
          mygrid.cells(rowId,1).setValue(100);
        }
     }
    return true;
}

function autoSuma(){
   var allId=mygrid.getAllRowIds().split(",");
				var sumaRowsLunes=0
                                var sumaRowsMartes=0
                                var sumaRowsMiercoles=0, sumaRowsJueves=0, sumaRowsViernes=0,sumaRowsSabado=0,sumaRowsDomingo=0,total=0

                                for(i=0;i<allId.length;i++){

                                 if (parseInt(mygrid.cells(allId[i],3).getValue())>=0){ //PAra descartar las filas que tienen titulo
				sumaRowsLunes+=         parseInt(mygrid.cells(allId[i],3).getValue())
                                sumaRowsMartes+=	parseInt(mygrid.cells(allId[i],4).getValue())
                                sumaRowsMiercoles+=	parseInt(mygrid.cells(allId[i],5).getValue())
                                sumaRowsJueves+=	parseInt(mygrid.cells(allId[i],6).getValue())
                                sumaRowsViernes+=	parseInt(mygrid.cells(allId[i],7).getValue())
                                sumaRowsSabado+=	parseInt(mygrid.cells(allId[i],8).getValue())
                                sumaRowsDomingo+=	parseInt(mygrid.cells(allId[i],9).getValue())
                                total+=	parseInt(mygrid.cells(allId[i],10).getValue())
		}}

                document.getElementById('inputLunes').value=sumaRowsLunes
                document.getElementById('inputMartes').value=sumaRowsMartes
                document.getElementById('inputMiercoles').value=sumaRowsMiercoles
                document.getElementById('inputJueves').value=sumaRowsJueves
                document.getElementById('inputViernes').value=sumaRowsViernes
                document.getElementById('inputSabado').value=sumaRowsSabado
                document.getElementById('inputDomingo').value=sumaRowsDomingo
                document.getElementById('inputTotal').value=total



}

  function submit(){
    var allId=mygrid.getAllRowIds();
    <%= remote_function :url => {:action => :submit}, :with => " 'id='+allId" %>
    updateStatus();
  }

  function nosubmit(){
    var allId=mygrid.getAllRowIds();
    <%= remote_function :url => {:action => :nosubmit}, :with => " 'id='+allId" %>
    initGrid();
    updateStatus();
  }

</script>


<center>
  <h1><%= @monday %></h1>
  A cargo de: <b><%= @user.complete_name %></b>
  <div id="statusDiv">Status: <script>document.write(status);</script></div><br/>


  <table width="100%" border="0">
  <tr><td align="center">
      <div id="gridbox" style="height:150px;width:990px"></div>
      <div id="resultadosBox" style="height:auto;width:990px">

        <table width="100%" border="0px">
          <tr>
            <td width="240px">
              Totales:
            </td>
            <td width="75px">
              <input type="text" class="inputs" value="" id="inputLunes" readonly/>
            </td>
            <td width="75px">
              <input type="text" class="inputs" value="" id="inputMartes" readonly/>
            </td>
            <td width="75px">
              <input type="text" class="inputs" value="" id="inputMiercoles" readonly/>
            </td>
            <td width="75px">
              <input type="text" class="inputs" value="" id="inputJueves" readonly/>
            </td>
            <td width="75px">
              <input type="text" class="inputs" value="" id="inputViernes" readonly/>
            </td>
            <td width="75px">
              <input type="text" class="inputs" value="" id="inputSabado"readonly/>
            </td>
            <td width="75px">
              <input type="text" class="inputs" value="" id="inputDomingo" readonly/>
            </td>
            <td width="50px">
              <input type="text" class="inputs" value="" id="inputTotal"readonly/>
            </td>

          </tr>

        </table>

      </div>
      <br/>      
  </td></tr>
</table><br/>
<%=link_to 'Aceptar','javascript:submit();'%> |
<%=link_to 'No Aceptar','javascript:nosubmit();'%> |
<%= link_to 'Regresar', :controller=>"accepted" %>
</center>

<script>
  try{
    Effect.toggle('subTasks','slide',{duration:0.0});
    Effect.toggle('subUsers','slide',{duration:0.0});
  }catch(e){

  }
  initGrid();
</script>